//
//  FirstViewController.m
//  QQViiew
//
//  Created by liuchengming on 16/9/28.
//  Copyright © 2016年 liuchengming. All rights reserved.
//

#import "FirstViewController.h"
#import "SecondViewController.h"
#import "LoginData.h"
#import "ViewController.h"

@interface FirstViewController ()
@property (weak, nonatomic) IBOutlet UITextField *UesrName;
@property (weak, nonatomic) IBOutlet UITextField *Password;

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CGRect rect = [UIScreen mainScreen].bounds;
    NSLog(@"%f,%f",rect.size.height,rect.size.width);
    
}

- (IBAction)onClick:(id)sender
{
    NSURL * url = [NSURL URLWithString:@"http://xl18z.cn/login.php?name=test&password=123"];
    NSURLRequest * urlrequest= [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionTask *task = [session dataTaskWithRequest:urlrequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
                              {
                                  //获取数据
                                  NSString * text = [[NSString alloc]initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
                                  NSLog(@"%@",text);
                                  NSDictionary * dick = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                                  LoginItemData * d = [[LoginItemData alloc]initWithDictionary:dick];
                                  NSLog(@"%@",d);
                                  //GCD 大中枢派发
                                  dispatch_async(dispatch_get_main_queue(),^{
                                      if ([dick[@"errcode"] intValue] == 0)
                                      {
                                          ViewController *con = [self.storyboard instantiateViewControllerWithIdentifier:@"first"];
                                          [self.navigationController pushViewController:con animated:YES];
                                          
                                          con.view.backgroundColor = [UIColor blueColor];
                                          
                                      }
                                      else{
                                          NSString *str = [dick objectForKey:@"errinfo"];
                                          UIAlertController *aler =[UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
                                          [self presentViewController:aler animated:YES completion:^{
                                              
                                          }];
                                          
                                          UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                                              
                                          }];
                                          [aler addAction:action];
                                          
                                      }
                                      
                                  });
                              }];
    [task resume];
}


//收回键盘
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_UesrName resignFirstResponder];
    [_Password resignFirstResponder];
}

- (void) onLine
{
    
}
- (IBAction)Exit:(UIBarButtonItem *)sender {
    exit(0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
